<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="站点名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入站点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用状态" prop="stationState">
        <el-select v-model="queryParams.stationState" placeholder="请选择启用状态" clearable>
          <el-option
            v-for="item in stationStateOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="测站级别" prop="stationLevel">
        <el-input
          v-model="queryParams.stationLevel"
          placeholder="请输入测站级别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="站网代码" prop="netCode">
        <el-input
          v-model="queryParams.netCode"
          placeholder="请输入站网代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区站号" prop="stationIdC">
        <el-input
          v-model="queryParams.stationIdC"
          placeholder="请输入区站号(字符)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['modules:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['modules:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['modules:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['modules:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="站点ID" align="center" prop="stationId" v-if="false"/>
      <el-table-column label="所属区域" align="center" prop="areaId" >
        <template slot-scope="scope">
          {{ areaName(scope.row.areaId) }}
        </template>
      </el-table-column>
      <el-table-column label="站点分类" align="center" prop="stationTypeId" >
        <template slot-scope="scope">
          {{ stationTypeName(scope.row.stationTypeId) }}
        </template>
      </el-table-column>
      <el-table-column label="站点名称" align="center" prop="stationName" />
      <el-table-column label="启用状态" align="center" prop="stationState">
        <template slot-scope="scope">
          {{ stationStateName(scope.row.stationState) }}
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="联系人" align="center" prop="contactPeopleName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="地市名" align="center" prop="city" />
      <el-table-column label="测站级别" align="center" prop="stationLevel" />
      <el-table-column label="行政编码" align="center" prop="adminCodeChn" />
      <el-table-column label="行政编码2" align="center" prop="vAcode4search" />
      <el-table-column label="乡镇名" align="center" prop="town" />
      <el-table-column label="国家代码" align="center" prop="countryCode" />
      <el-table-column label="省名" align="center" prop="province" />
      <el-table-column label="区域代码" align="center" prop="regionCode" />
      <el-table-column label="站网代码" align="center" prop="netCode" />
      <el-table-column label="国家名称" align="center" prop="country" />
      <el-table-column label="区县名" align="center" prop="cnty" />
      <el-table-column label="区站号" align="center" prop="stationIdC" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="经度" align="center" prop="lon" />
      <el-table-column label="测站高度" align="center" prop="alti" />
      <el-table-column label="镇编码" align="center" prop="townCode" />
      <el-table-column label="监测时间间隔" align="center" prop="monitorTimeInterval" />
      <el-table-column label="维护时间" align="center" prop="maintainTime" />
      <el-table-column label="上级行政编码" align="center" prop="parentCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['modules:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['modules:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改站点信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="所在区域" prop="areaId">
              <treeselect v-model="form.areaId" :options="areaOptions" :show-count="true" placeholder="请选择所在区域" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="站点分类" prop="stationTypeId">
              <treeselect v-model="form.stationTypeId" :options="stationTypeOptions" :show-count="true" placeholder="请选择站点分类" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="站点名称" prop="stationName">
            <el-input v-model="form.stationName" placeholder="请输入站点名称" />
          </el-form-item>
          </el-col>
          <el-col :span="9">
          <el-form-item label="启用状态" prop="stationState">
            <el-select v-model="form.stationState" placeholder="请选择启用状态">
              <el-option
                v-for="item in stationStateOptions"
                :key="item.key"
                :label="item.value"
                :value="item.key">
              </el-option>
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="15">
          <el-form-item label="电话" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入电话" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="联系人" prop="contactPeopleName">
            <el-input v-model="form.contactPeopleName" placeholder="请输入联系人" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="地市名" prop="city">
            <el-input v-model="form.city" placeholder="请输入地市名" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="测站级别" prop="stationLevel">
            <el-input v-model="form.stationLevel" placeholder="请输入测站级别" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="行政编码" prop="adminCodeChn">
            <el-input v-model="form.adminCodeChn" placeholder="请输入行政编码" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="行政编码2" prop="vAcode4search">
            <el-input v-model="form.vAcode4search" placeholder="请输入行政编码2" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="乡镇名" prop="town">
            <el-input v-model="form.town" placeholder="请输入乡镇名" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="国家代码" prop="countryCode">
            <el-input v-model="form.countryCode" placeholder="请输入国家代码" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="省名" prop="province">
            <el-input v-model="form.province" placeholder="请输入省名" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="区域代码" prop="regionCode">
            <el-input v-model="form.regionCode" placeholder="请输入区域代码" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="站网代码" prop="netCode">
            <el-input v-model="form.netCode" placeholder="请输入站网代码" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="国家名称" prop="country">
            <el-input v-model="form.country" placeholder="请输入国家名称" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="区县名" prop="cnty">
            <el-input v-model="form.cnty" placeholder="请输入区县名" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="区站号" prop="stationIdC">
            <el-input v-model="form.stationIdC" placeholder="请输入区站号(字符)" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="纬度" prop="lat">
            <el-input v-model="form.lat" placeholder="请输入纬度" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="经度" prop="lon">
            <el-input v-model="form.lon" placeholder="请输入经度" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="测站高度" prop="alti">
            <el-input v-model="form.alti" placeholder="请输入测站高度" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="镇编码" prop="townCode">
            <el-input v-model="form.townCode" placeholder="请输入镇编码" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="监测时间间隔" prop="monitorTimeInterval">
            <el-input v-model="form.monitorTimeInterval" placeholder="请输入监测时间间隔" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="维护时间" prop="maintainTime">
            <el-input v-model="form.maintainTime" placeholder="请输入维护时间" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
          <el-form-item label="上级行政编码" prop="parentCode">
            <el-input v-model="form.parentCode" placeholder="请输入上级行政编码" />
          </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/modules/info";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listArea,treeselect as areaTreeSelect } from "@/api/modules/area";
import { listStationType,treeselect as stationTypeTreeSelect } from "@/api/modules/stationType";

export default {
  name: "Info",
  components: { Treeselect },
  data() {
    return {
      // 所有区域列表
      allAreaList:[],
      // 所有站点分类列表
      allStationTypeList:[],
      // 区域列表
      areaOptions: [],
      // 站点分类列表
      stationTypeOptions:[],
      stationStateOptions:[
        {"key":1,value:'启用'},
        {"key":2,value:'不启用'}
        ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationTypeId: undefined,
        areaId: undefined,
        stationName: undefined,
        stationState: undefined,
        phone: undefined,
        contactPeopleName: undefined,
        city: undefined,
        stationLevel: undefined,
        adminCodeChn: undefined,
        vAcode4search: undefined,
        town: undefined,
        countryCode: undefined,
        province: undefined,
        regionCode: undefined,
        netCode: undefined,
        country: undefined,
        cnty: undefined,
        stationIdC: undefined,
        lat: undefined,
        lon: undefined,
        alti: undefined,
        townCode: undefined,
        monitorTimeInterval: undefined,
        maintainTime: undefined,
        parentCode: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.init();
  },
  methods: {
    /** 初始化 */
    init(){
      listArea().then( res =>{
        this.allAreaList = res.data;
      })
      let area = {
        areaState:0
      }
      areaTreeSelect(area).then(response => {
        this.areaOptions = response.data;
      });
      listStationType().then( res =>{
        this.allStationTypeList = res.data;
      })
      let stationType = {
        stationTypeState:0
      }
      stationTypeTreeSelect(stationType).then(response => {
        this.stationTypeOptions = response.data;
      });
    },
    /** 回显站点状态 */
    stationStateName(key){
      let value = '';
      this.stationStateOptions.forEach(state =>{
        if(state.key === key){
          value = state.value;
        }
      });
      return value;
    },
    /** 回显区域名称 */
    areaName(areaId){
      let value = '';
      this.allAreaList.forEach(area =>{
        if(area.areaId === areaId){
          value = area.areaName;
        }
      });
      return value;
    },
    /** 回显站点分类名称 */
    stationTypeName(stationTypeId){
      let value = '';
      this.allStationTypeList.forEach(stationType =>{
        if(stationType.stationTypeId === stationTypeId){
          value = stationType.stationTypeName;
        }
      });
      return value;
    },

    /** 查询站点信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stationId: undefined,
        stationTypeId: undefined,
        areaId: undefined,
        stationName: undefined,
        stationState: undefined,
        phone: undefined,
        contactPeopleName: undefined,
        remark: undefined,
        city: undefined,
        stationLevel: undefined,
        adminCodeChn: undefined,
        vAcode4search: undefined,
        town: undefined,
        countryCode: undefined,
        province: undefined,
        regionCode: undefined,
        netCode: undefined,
        country: undefined,
        cnty: undefined,
        stationIdC: undefined,
        lat: undefined,
        lon: undefined,
        alti: undefined,
        townCode: undefined,
        monitorTimeInterval: undefined,
        maintainTime: undefined,
        parentCode: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stationId = row.stationId || this.ids
      getInfo(stationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站点信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stationId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stationIds = row.stationId || this.ids;
      this.$confirm('是否确认删除站点信息编号为"' + stationIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delInfo(stationIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有站点信息数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportInfo(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
